﻿/*************************************************************
 * Software of blackcore
 *
 * CLRVersion:4.0.30319.586
 * MachineName:SPACE
 * Author:Andy Huang
 * Email:itblackhole@gmail.com
 * DateTime：6/26/2013 2:46:21 PM
 * Function：
 *************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SobFS.Common;
using System.Text;

namespace SobFS.Web.backend.manage.usergroup
{
    public partial class List : SobFS.Web.UI.BackendPage
    {
        #region ******************************Private Property******************************
        private const string COOKIE_BACKEND_MNGUSERGROUP_PAGESIZE = "COOKIE_BACKEND_MNGUSERGROUP_PAGESIZE";
        private int totalCount;
        private int page;
        private int pageSize;
        private string queryKeyword;
        #endregion ******************************Private Property******************************


        #region ******************************Public Property******************************
        #endregion ******************************Public Property******************************


        #region ******************************Structure******************************
        protected void Page_Load(object sender, EventArgs e)
        {
            this.queryKeyword = RequestHelper.GetQueryString("queryKeyword");
            this.pageSize = GetPageSize(15);
            if (!Page.IsPostBack)
            {
                //TODO 权限检查                    
                DataBind("IsObsoleted = 0 " + CombinSqlCondition(this.queryKeyword), "Id");
            }
        }
        #endregion  ******************************Structure******************************


        #region ******************************Private Method******************************
        int GetPageSize(int defaultSize)
        {
            int _pagesize;
            if (int.TryParse(CookieHelper.ReaderCookie(COOKIE_BACKEND_MNGUSERGROUP_PAGESIZE), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return defaultSize;
        }

        string CombinSqlCondition(string querykeyword)
        {
            StringBuilder queryCondition = new StringBuilder();
            querykeyword = querykeyword.Replace("'", "");
            if (!string.IsNullOrEmpty(querykeyword))
            {
                queryCondition.Append(" AND(");
                queryCondition.Append(" Name LIKE '%" + querykeyword + "%'");
                queryCondition.Append(" )");
            }
            return queryCondition.ToString();
        }

        void DataBind(string queryCondition, string orderBy)
        {
            this.page = RequestHelper.GetQueryInt("page", 1);
            this.txtKeywords.Text = this.queryKeyword;
            this.rptList.DataSource = new BLL.SysGroup().GetList(this.pageSize, this.page, queryCondition, orderBy, out this.totalCount);
            this.rptList.DataBind();
            //绑定页码
            txtPageNum.Text = this.pageSize.ToString();
            string pageUrl = SobFSAssistant.CombineUrlTxt("List.aspx", "queryKeyword={0}&page={1}", this.queryKeyword, "__id__");
            PageContent.InnerHtml = SobFSAssistant.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
        }
        #endregion ******************************Private Method******************************



        #region ******************************Public Method******************************
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            BLL.SysGroup bll = new BLL.SysGroup();
            List<int> ids = new List<int>();
            for (int i = 0; i < this.rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                CheckBox cbox = (CheckBox)rptList.Items[i].FindControl("chkId");
                if (cbox.Checked)
                {
                    ids.Add(id);
                }
            }
            bll.DeleteList(string.Join(",", ids));
            JsMessageBox("操作成功！", SobFSAssistant.CombineUrlTxt("List.aspx", "queryKeyword={0}", this.queryKeyword), UI.JsMessageBoxCss.Success);
        }

        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int _pagesize;
            if (int.TryParse(this.txtPageNum.Text.Trim(), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    CookieHelper.WriterCookie(COOKIE_BACKEND_MNGUSERGROUP_PAGESIZE, _pagesize.ToString(), 43200);
                }
            }
            Response.Redirect(SobFSAssistant.CombineUrlTxt("List.aspx", "queryKeyword={0}", this.queryKeyword));
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect(SobFSAssistant.CombineUrlTxt("List.aspx", "queryKeyword={0}", this.txtKeywords.Text.Trim()));
        }
        #endregion ******************************Public Method******************************
    }
}